<template>
  <div class="login-page">
    <el-col :offset="12" v-if="!regi">
    <el-card>
      <template #header>登录界面</template>
      <el-form autocomplete="off" ref="form" :rules="rules" :model="form">
        <el-form-item label="用户名" prop="email">
          <el-input v-model="form.email" placeholder="输入用邮箱"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="输入用户密码"></el-input>
        </el-form-item>

        <el-form-item class="tc">
          <el-button type="primary" @click="login(form)">登 录</el-button>
          <el-button @click="reg()" >注 册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    </el-col>


    <div v-if="regi">
    <el-col :offset="12">
    <el-card style="width : 550px;">
      <template #header>注册页面</template>
      <hr>
      <el-form autocomplete="off" ref="form" :rules="rules2" :model="form" >
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="输入用邮箱"></el-input>
        </el-form-item>

        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="输入用户密码"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="password2">
          <el-input v-model="form.password2" type="password" placeholder="请再次输入用户密码"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="输入手机号"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="4" v-if="!buttonView" >
            <el-button  type="primary" :loading="!buttonView">等待30秒</el-button>
          </el-col>
          <el-col :span="4" v-if="buttonView">
            <el-button  type="primary"  @click="send" > 发送验证码</el-button>
          </el-col>
          <el-col :span="6" :offset="2">
            <el-form-item  prop="code">
            <el-input v-model="form.code" placeholder="输入验证码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <el-form-item class="tc">
          <el-button type="primary" @click="register()">注 册</el-button>
          <el-button @click="reg()" >退 回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    </el-col>
    </div>
  </div>
</template>

<script>
import {login} from '@/api/user'
import {register} from '@/api/user'
import {sendCode} from '@/api/user'
export default {
  name: 'login-page',
  data () {
    return {
      //检测是否为注册
      regi: false,
      form: {email: '',password: '',password2: '',phone: '',userName: '',code: ''},
      rules: {
       email: [
        { required: true, message: '请输入用户邮箱', trigger: ['blur', 'change'] },
        { max: 100, message: '最大长度100字符', trigger: ['blur', 'change'] }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
        { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
      ]
      },
      rules2: {
       email: [
        { required: true, message: '请输入用户邮箱', trigger: ['blur', 'change'] },
        { max: 100, message: '最大长度100字符', trigger: ['blur', 'change'] }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
        { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
      ],
      userName: [
        { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
      ],
      password2: [
        { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
        { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] },

        {validator: (rule, value, callback) => {
         if (value !== this.form.password) {
         callback(new Error('两次输入密码不一致'));
         } else {
          callback();
         }
       },
            trigger: ['blur', 'change'] }
      ],
      phone: [
        { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
        { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确手机号', trigger: ['blur', 'change'] },
      ]
      },
      //用于发送验证码按钮的显示
      buttonView: true

    }
  },
  methods: {
    // 表单重置
    rest () {
      this.form = {}
      this.$refs.form.resetFields()
    },
    //登录
    login (data) {
      login(data).then((res)=>{
        const temp = res.data
        if(temp.code == 0){
          this.$store.commit("user/set",temp.data.token)
          localStorage.setItem("user",JSON.stringify(temp.data))
          this.$router.push('/')
        }
      })
    },
    //点击注册按钮
    reg () {
      this.regi = !this.regi
      this.rest()
    },
    // 注册
    register () {
      register(this.form).then((res)=>{
        if(res.data.code == 0){
          this.$message.success("注册成功")
          this.reg()
        }
      })
    },
    //发送验证码30秒后才可再发送
    send () {
      if(this.form.email != ''){
        sendCode(this.form.email)
        this.buttonView = !this.buttonView
        setTimeout(() => {
          this.buttonView = !this.buttonView
        }, 30000);
      }else{
         this.$message('邮箱不能为空');
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login.jpg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background: #3E88B7;
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
